<!-- 订单卡片组件 -->
<template>
  <div class="order-card">
    <div class="order-title">
      <div class="shop-name">{{ orderInfo.shopName }}</div>
      <div class="order-status">{{ orderInfo.isCanceled ? '已取消' : '已下单' }}</div>
    </div>
    <div class="order-content">
      <div class="order-content-img">
        <img v-for="(item, index) in orderInfo.imgList" :key="index" :src="item.product.img">
      </div>
      <div class="order-content-info">
        <div class="price">￥{{ orderInfo.price }}</div>
        <div class="total">共{{ orderInfo.total }}件</div>
      </div>
    </div>
  </div>
</template>

<script>
// 处理数据格式
const useDataFormat = (data) => {
  let total = 0, price = 0;
  const orderInfo = data;
  if (orderInfo.products.length > 4) orderInfo.imgList = orderInfo.products.slice(0, 4);
  else orderInfo.imgList = orderInfo.products;
  for (let item of orderInfo.products) {
    total += item.orderSales;
    price += item.orderSales * item.product.price;
  }
  orderInfo.total = total;
  orderInfo.price = price;
  return { orderInfo }
}
//composition
export default {
  name: 'orderCard',
  setup(props, context) {
    const { attrs } = context;
    const { orderInfo } = useDataFormat(attrs.order)
    return { orderInfo }
  }
}
</script>

<style scoped lang="scss">
@import '@/styles/theme.scss';

.order-card {
  background-color: $white;
  margin-bottom: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  border-radius: 4px;

  .order-title {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16px;

    .shop-name {
      font-size: 16px;
      font-weight: bold;
      color: $theme-font-color;
    }

    .order-status {
      font-size: 14px;
      color: #999999;
    }
  }

  .order-content {
    display: flex;
    justify-content: space-between;

    &-img {
      display: flex;

      img {
        width: 10.6667vw;
        height: 10.6667vw;
        margin-right: 3.2vw;
      }
    }

    &-info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-end;

      .price {
        font-size: 14px;
        color: $remind-color;
      }

      .total {
        font-size: 12px;
        color: $theme-font-color;
      }
    }
  }
}
</style>